/******************************************************************************
* file  : console.c
* creat : apleilx
* data  : 2021/01/20
*******************************************************************************/
#include "bsp_api.h"
#include "os_obj.h"
#include "sys_task.h"
#include "com_obj.h"
#include "dataproc.h"
#include "common.h"
#include "simp_txt_analysis.h"
#include "password.h"
#include "iohandle.h"
#include "multi_malloc.h"
#include "console_cmd.h"

uint32_t cmd_back_dat[16];
void cmd_callback(uint16_t cmd_site, const char * txt_src, uint16_t len, int16_t cmd_num);


#define CMD_ATTR_ADMIN			1
#define CMD_ATTR_FACTORY		(CMD_ATTR_ADMIN + 1)
#define CMD_ATTR_PASSWORD		(CMD_ATTR_FACTORY + 1)
#define CMD_ATTR_CMD			(CMD_ATTR_PASSWORD + 1)
#define CMD_ATTR_LUT_RD			(CMD_ATTR_CMD + 1)
#define CMD_ATTR_LUT_WR			(CMD_ATTR_LUT_RD + 1)
#define CMD_ATTR_RS				(CMD_ATTR_LUT_WR + 1)
#define CMD_ATTR_DATE_R			(CMD_ATTR_RS + 1)
#define CMD_ATTR_DATE_S			(CMD_ATTR_DATE_R + 1)
#define CMD_ATTR_DATE			(CMD_ATTR_DATE_S + 1)
#define CMD_ATTR_LOG			(CMD_ATTR_DATE + 1)
#define CMD_ATTR_CORE_MARK		(CMD_ATTR_LOG + 1)
#define CMD_ATTR_READ		    (CMD_ATTR_CORE_MARK + 1)
#define CMD_ATTR_CLR			(CMD_ATTR_READ + 1)
#define CMD_ATTR_SDFAT			(CMD_ATTR_CLR + 1)
#define CMD_ATTR_USBFAT			(CMD_ATTR_SDFAT + 1)
#define CMD_ATTR_KEY			(CMD_ATTR_USBFAT + 1)
#define CMD_ATTR_USER			(CMD_ATTR_KEY + 1)
#define CMD_ATTR_TASK			(CMD_ATTR_USER + 1)
#define CMD_ATTR_MUSED			(CMD_ATTR_TASK + 1)
#define CMD_ATTR_PWM			(CMD_ATTR_MUSED + 1)
#define CMD_ATTR_CAP			(CMD_ATTR_PWM + 1)
#define CMD_ATTR_SIZE			(CMD_ATTR_CAP + 1)
#define CMD_ATTR_END			127

#if CMD_ATTR_SIZE != 23
#error cfg cmd define error "at usb_file_cfg.c"
#endif

/*-----------------------------------------------------------------------------*
* bit 15    : not display in info.txt
* bit 14    : not display in any file
* bit 8-11  : 0 无需权限， 1-管理权限， 2-工厂权限
*-----------------------------------------------------------------------------*/
const txt_cmd_cfg_type cmd_list[] =
{
	{ 0x4000 | CMD_ATTR_ADMIN, "ADMIN.LOGIN", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback },
	{ 0x4000 | CMD_ATTR_FACTORY, "FACTORY.LOGIN", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback },
	{ 0x4100 | CMD_ATTR_PASSWORD, "PASSWORD", TXT_CMD_TYPE_STRING, cmd_back_dat, 16, cmd_callback },
	{ 0x8000 | CMD_ATTR_CMD, "CMD", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },

	{ 0x0000 | CMD_ATTR_LUT_RD, "LUTRD", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback },
	{ 0x0000 | CMD_ATTR_LUT_WR, "LUTWR", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback },
	{ 0x0000 | CMD_ATTR_RS, "RS", TXT_CMD_TYPE_NUMBER, cmd_back_dat, 1, cmd_callback },
	{ 0x0000 | CMD_ATTR_DATE_R, "DATE.R", TXT_CMD_TYPE_SHORT, cmd_back_dat, 3, cmd_callback },
	{ 0x0000 | CMD_ATTR_DATE_S, "DATE.S", TXT_CMD_TYPE_SHORT, cmd_back_dat, 3, cmd_callback },
	{ 0x0000 | CMD_ATTR_DATE, "DATE", TXT_CMD_TYPE_SHORT, cmd_back_dat, 6, cmd_callback },
	{ 0x0000 | CMD_ATTR_LOG, "LOG", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x0000 | CMD_ATTR_CORE_MARK, "COREMARK", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },

	{ 0x8100 | CMD_ATTR_CLR, "CLR", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_READ, "READ", TXT_CMD_TYPE_STRING, cmd_back_dat, 50, cmd_callback },
	{ 0x8100 | CMD_ATTR_SDFAT, "SDFAT", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback },
	{ 0x8100 | CMD_ATTR_USBFAT, "USBFAT", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback },
	{ 0x8100 | CMD_ATTR_KEY, "KEY[]", TXT_CMD_TYPE_CHAR, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_USER, "USER", TXT_CMD_TYPE_SHORT, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_TASK, "OS-TASK", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_MUSED, "MUSED", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ 0x8100 | CMD_ATTR_PWM, "PWM", TXT_CMD_TYPE_LONG, cmd_back_dat, 2, cmd_callback },
	{ 0x8100 | CMD_ATTR_CAP, "CAP", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback },
	{ CMD_ATTR_END, "END", TXT_CMD_TYPE_END, cmd_back_dat, 1, cmd_callback },
};


/*******************************************************************************
* @brief  cmd_callback.
* \param[in] none
* \retval: none
*******************************************************************************/
void cmd_callback(uint16_t cmd_site, const char * txt_src, uint16_t len, int16_t cmd_num)
{
	char *i8p;
	int16_t *i16p;
	int32_t *i32p;

	if (cmd_site > sizeof(cmd_list) / sizeof(txt_cmd_cfg_type))
		return;

	i8p = cmd_list[cmd_site].des;
	i16p = cmd_list[cmd_site].des;
	i32p = cmd_list[cmd_site].des;

	(void) i8p;
	(void) i16p;
	(void) i32p;
	
	switch (cmd_list[cmd_site].attr & 0xFF)
	{
		case CMD_ATTR_ADMIN:
		{
			dbg_puts("CMD_ADMIN\n");
			break;
		}
		case CMD_ATTR_FACTORY:
		{
			dbg_puts("CMD_FACTORY\n");
			break;
		}
		case CMD_ATTR_PASSWORD:
		{
			char pwd[12], pwsrc[32];
			int src_len = strlen(i8p);

			dbg_print("PASSWORD SRC LEN = %d \n", src_len);

			strcpy(pwsrc, i8p);
			pwsrc[src_len] = 0;
			string6_passward_creat(pwsrc, pwd, 1);
			dbg_print("PASSWORD 6 string = %s \n", pwd);

			strcpy(pwsrc, i8p);
			pwsrc[src_len] = 0;
			string8_passward_creat(pwsrc, pwd, 1);
			dbg_print("PASSWORD 8 string = %s \n", pwd);

			strcpy(pwsrc, i8p);
			pwsrc[src_len] = 0;
			string10_passward_creat(pwsrc, pwd, 1);
			dbg_print("PASSWORD 10 string = %s \n", pwd);

			break;
		}
		case CMD_ATTR_CMD:
		{
			break;
		}
		case CMD_ATTR_LUT_RD:
		{
			//lut_read_req(*i32p);
			break;
		}
		case CMD_ATTR_LUT_WR:
		{
			//lut_write_req(*i32p);
			break;
		}
		case CMD_ATTR_RS:
		{
			break;
		}
		case CMD_ATTR_DATE_R:
		{
			break;
		}
		case CMD_ATTR_DATE_S:
		{
			break;
		}
		case CMD_ATTR_DATE:
		{
			sig_time_type tm;
			
			tm.time.year = i16p[0];
			tm.time.mon = i16p[1];
			tm.time.day = i16p[2];
			tm.time.hour = i16p[3];
			tm.time.min = i16p[4];
			tm.time.sec2t = i16p[5]>>1;
			
			dbg_print("tm = %2d %2d %2d %2d %2d %2d => %d\n", 
					i16p[0],i16p[1],i16p[2],i16p[3],i16p[4],i16p[5],
					sig_time_to_min(tm));
			
			(void) tm;
			break;
		}
		case CMD_ATTR_LOG:
		{
			break;
		}
		case CMD_ATTR_CORE_MARK:
		{
			break;
		}
		case CMD_ATTR_CLR:
		{
			break;
		}
		case CMD_ATTR_READ:
		{
			//dbg_print("read file : %s\n", i8p);
			//copy_str(i8p, sys_st.cmd.fil_name, 32);
			//sys_st.cmd.fread_req = 1;
			break;
			break;
		}
		case CMD_ATTR_SDFAT:
		{
			//sys_st.cmd.copy_sd_to_afat = (atoi(i8p) & 0x0F) + (cmd_num << 4);
			break;
		}
		case CMD_ATTR_USBFAT:
		{
			//sys_st.cmd.copy_sd_to_afat = (atoi(i8p) & 0x0F) + (cmd_num << 4);
			break;
		}
		case CMD_ATTR_KEY:
		{
			break;
		}
		case CMD_ATTR_USER:
		{
			break;
		}
		case CMD_ATTR_TASK:
		{
			os_task_info();
			break;
		}
		case CMD_ATTR_MUSED:
		{
			uu8 buff[10];
			i32_to_string(buff, sys_st.info.stack_used, 0x31, 3);
			dbg_print("system stack used : %s%%\n", buff);
			break;
		}
		case CMD_ATTR_PWM:
		{
			break;
		}
		case CMD_ATTR_CAP:
		{
			//dbg_print("cap period = %d us, duty = %d us\n", cap_period_get(), cap_duty_get());
			break;
		}
		default:
		{
			break;
		}
	}
}
